<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :form_data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
          header_list[e].value =
            header_list[e].type == 'input_number' ? ['', ''] : ''
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div v-if="route_name == 'adminWorkHours'">工时管理</div>
        <div v-else>我的工时</div>
        <div class="flex">
          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        border
        v-loading="loading"
        :row-style="{
          height:
            height_size == 1 ? '58px' : height_size == 2 ? '48px' : '38px',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="项目名称"
          prop="projectName"
          key="项目名称"
          sortable
          v-if="header_list[0].showTable"
        ></el-table-column>
        <el-table-column
          label="任务名称"
          prop="taskName"
          key="任务名称"
          sortable
          v-if="header_list[1].showTable"
        ></el-table-column>
        <el-table-column
          label="子任务名称"
          prop="assignTaskNodeName"
          key="子任务名称"
          sortable
          v-if="header_list[2].showTable"
        ></el-table-column>

        <el-table-column
          label="日期"
          prop="taskCode"
          key="日期"
          sortable
          v-if="header_list[3].showTable"
        >
          <template #default="scope">
            <div v-if="scope.row.recordTime ?? '' !== ''">
              {{ formatDate(parseInt(scope.row.recordTime), 'YYYY-MM-DD') }}
            </div>
            <div v-else>-- --</div>
          </template>
        </el-table-column>
        <el-table-column
          label="工时"
          prop="workHours"
          key="工时"
          sortable
          v-if="header_list[4].showTable"
        >
        </el-table-column>
        <el-table-column
          label="工时类型"
          prop="workHours"
          key="工时类型"
          sortable
          v-if="header_list[5].showTable"
        >
          <template #default="scope">
            <div v-if="scope.row.taskRecordType == 2">额外工时</div>
            <div v-if="scope.row.taskRecordType == 1">任务工时</div>
          </template></el-table-column
        >
        <el-table-column
          label="当前总工时"
          prop="usedHours"
          key="当前总工时"
          sortable
          v-if="header_list[6].showTable"
        >
          <template #default="scope">
            <div>
              {{ scope.row.usedHours }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="当前任务状态"
          key="当前任务状态"
          sortable
          prop="isDelete"
          v-if="header_list[7].showTable"
        >
          <template #default="scope">
            <div v-if="scope.row.assignTaskStatus == 1">未开始</div>
            <div v-if="scope.row.assignTaskStatus == 2">进行中</div>
            <div v-if="scope.row.assignTaskStatus == 3">延期</div>
            <div v-if="scope.row.assignTaskStatus == 4">已完成</div>
            <div v-if="scope.row.assignTaskStatus == 5">超时完成</div>
          </template></el-table-column
        >
        <el-table-column
          label="备注"
          key="备注"
          sortable
          prop="remark"
          show-overflow-tooltip
          v-if="header_list[8].showTable"
        ></el-table-column>
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="list_debounce()"
        @current-change="list_debounce()"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import {
  check_role,
  formatDate,
  debounce,
  handle_search_key,
} from '@/utils/check_role' // 权限校验
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted } from 'vue'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
import { ElMessage } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const list = ref()
const total = ref()
const max_height = ref()
const loading = ref(false)
const height_size = ref(1)
const header_list = ref<any>([
  {
    label: '项目名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '任务名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '子任务名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '日期',
    value: '',
    showTable: true,
    type: 'date',
    isShow: false,
    options: [],
  },
  {
    label: '工时',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '工时类型',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      {
        value: '1',
        label: '任务工时',
      },
      {
        value: '2',
        label: '额外工时',
      },
    ],
  },
  {
    label: '当前总工时',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '当前任务状态',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      {
        value: '1',
        label: '未开始',
      },
      {
        value: '2',
        label: '进行中',
      },
      {
        value: '3',
        label: '延期',
      },
      {
        value: '4',
        label: '已完成',
      },
      {
        value: '5',
        label: '超时完成',
      },
    ],
  },
  {
    label: '备注',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
])
const page_data = ref<any>({
  current: 1,
  pageSize: 10,
  projectName: '',
  taskName: '',
  assignTaskNodeName: '',
  assignTaskStatus: '',
  taskRecordType: '',
  recordTime: '',
  workHours: '',
  usedHours: '',
  remark: '',
})

const route_name = ref<any>('')
onMounted(() => {
  console.log(route.query)
  if (route.query) {
    page_data.value.projectName = route.query.pro_name
    page_data.value.taskName = route.query.task_name
    page_data.value.assignTaskNodeName = route.query.node_name
    header_list.value[0].value = route.query.pro_name
    header_list.value[1].value = route.query.task_name
  }
  max_height.value = window.innerHeight - 320 // 带分页的 table 高度
  EventBus.on('refresh', () => {
    get_list()
    ElMessage.success('刷新成功！')
  })
  EventBus.on('change_default_active', (e: any) => {
    console.log(e)
    route_name.value = e.split('/')[1]
    get_list()
  })
  console.log(router.currentRoute.value.name)
  route_name.value = router.currentRoute.value.name
  get_list()
  //   get_myProjectTaskList();
})
onUnmounted(() => {
  EventBus.off('refresh')
  EventBus.off('change_default_active')
})
const get_list = () => {
  loading.value = true
  axios
    .request({
      url:
        route_name.value == 'adminWorkHours'
          ? serviceApi.taskMemberRecordPage
          : serviceApi.myWorkHours,
      method: 'post',
      data: handle_search_key(page_data.value),
    })
    .then((res) => {
      console.log(res)
      list.value = res.data.content
      total.value = parseInt(res.data.totalElements)
      loading.value = false
    })
    .catch((err) => {
      ElMessage.error(err)
      loading.value = false
    })
}
let list_debounce = debounce(200, get_list)
const search_key = () => {
  page_data.value = {
    current: 1,
    pageSize: 10,
    projectName: header_list.value[0].value,
    taskName: header_list.value[1].value,
    assignTaskNodeName: header_list.value[2].value,
    recordTime: header_list.value[3].value,
    workHours: header_list.value[4].value,
    taskRecordType: header_list.value[5].value,
    usedHours: header_list.value[6].value,
    assignTaskStatus: header_list.value[7].value,
    remark: header_list.value[8].value,
  }
  get_list()
}
</script>

<style lang="scss" scoped>
.top_item {
  border: 1px solid #ccc;
  cursor: pointer;
  padding: 5px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  &:hover {
    color: #3580eb;
    border-color: #3580eb;
  }
}
.line {
  background-color: #333;
  width: 1px;
  height: 16px;
  margin: 0 10px;
}
</style>
